package com.boxuegu.service;

import com.boxuegu.controller.CardController;
import com.boxuegu.controller.LineController;
import com.boxuegu.dao.CardDao;
import com.boxuegu.dao.LineDao;
import com.boxuegu.domain.Card;
import com.boxuegu.domain.Station;
import com.boxuegu.service.CardService;
import javax.swing.text.Caret;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.IOException;
public class  LineService {
    private static Object Station;

    public void buyTicket() throws IOException {
        //查询乘车路线方法调用，返回count

        Scanner sc = new Scanner(System.in);
        loop:
        while (true) {
            System.out.println("请输入出发站");
            String departureStation = sc.next();
            //判断出发展是否存在
            boolean b1 = isStationExist(departureStation);
            if (b1) {
                while (true) {
                    System.out.println("请输入目的站");
                    String destinationStation = sc.next();
                    //判断目的站是否存在
                    boolean b2 = isStationExist(destinationStation);
                    if (b2) {
                        //都存在则调用查询路线的方法打印路线以及返回站点总数用于扣款
                        int count = accept(departureStation, destinationStation);

                        Scanner idnum = new Scanner(System.in);
                        CardDao cardDao = new CardDao();
                        while (true) {
                            System.out.println("请输入您的卡号：(如需注册新用户请输入addUser,返回输入r)");
                            String s = idnum.next();
                            if ("addUser".equals(s)) {
                                //addUser注册
//
                                CardController cardController = new CardController();
                                cardController.addUser();

                            }else if("r".equals(s)) {
                                LineController lineController = new LineController();
                                lineController.start();
                                break loop;
                            }
                            else {
                                //判断卡号是否存在
                                CardService cardService = new CardService();
                                boolean b = cardService.isExists(s);
                                if (b) {
                                    //存在 调用扣款方法
                                    cardDao.cutPayment(s, count);
                                    LineController lineController = new LineController();
                                    lineController.start();
                                    break loop;
                                } else {
                                    System.out.println("您输入的卡号有误，请重新输入（返回上一层请输入 r）");
                                    Scanner ss = new Scanner(System.in);
                                    String line = sc.next();
                                    if ("r".equals(line)) {
                                        LineController lineController = new LineController();
                                        lineController.start();
                                        break loop;
                                    }

                                }
                            }
                        }
                    } else {
                        System.out.println("您输入的目的站有误，请重新输入（返回上一层请输入 r）");
                        Scanner ss = new Scanner(System.in);
                        String line = sc.next();
                        if ("r".equals(line)) {
                            LineController lineController = new LineController();
                            lineController.start();
                            break loop;
                        }
                    }
                }
            } else {
                System.out.println("您输入的出发站有误，请重新输入（返回上一层请输入 r）");
                Scanner ss = new Scanner(System.in);
                String line = sc.next();
                if ("r".equals(line)) {
                    LineController lineController = new LineController();
                    lineController.start();
                    break loop;
                }
            }
        }

    }


    //判断输入的站点是否存在
    public static boolean isStationExist(String station) throws IOException {
        String[] ss1 = LineDao.arrayString();
        int exist = -1;
        for (int j = 0; j < ss1.length; j++) {
            if (station.equals(ss1[j])) {
                exist = 1;
                break;
            }
        }
        if (exist == 1) {
            return true;
        } else {
            return false;
        }
    }

    //进行输入上站和下站信息以及打印乘车路线信息
    public static void routeMap() throws IOException {
        String station = null;
        String start = null;
        String end = null;
        //Scanner
        Scanner sc = new Scanner(System.in);
        //while循环确保
        lo:
        while (true) {
            System.out.println("输入您的上车站");
            String s1 = sc.next();
            //判断是否存在
            boolean i = LineService.isStationExist(s1);
            if (i) {
                start = s1;
                break lo;
            } else {
                System.out.println("您输入的信息有误，请重新输入");
            }
        }
        //System.out.println(start);

        //输入乘车路线的终点站
        lp:
        while (true) {
            System.out.println("输入您的下车站");
            String s2 = sc.next();
            //判断是否存在
            boolean j = LineService.isStationExist(s2);
            if (j) {
                end = s2;
                break lp;
            } else {
                System.out.println("您输入的信息有误，请重新输入");
            }
        }
        //System.out.println(end);
        LineService.accept(start, end);
    }

    //接收起始站点和终点站点，并调用LineDao内的方法并返回结果
    public static int accept(String start, String end) throws IOException {
        int result = LineDao.line2(start, end);
        return result;

    }

    /**
     * 业务员获取到所有路线信息
     *
     * @return 将存储所有站点信息的集合, 返回给前台
     */
    public ArrayList<Object> findAllRoutes() {
        LineDao lineDao = new LineDao();
        ArrayList<Object> allRoutesList = lineDao.findAllRoutes();
        return allRoutesList;
    }

}